home *** CD-ROM | disk | FTP | other *** search
- package com.sun.java.swing.plaf.basic;
-
- import com.sun.java.swing.Icon;
- import com.sun.java.swing.JComponent;
- import com.sun.java.swing.JLabel;
- import com.sun.java.swing.JTree;
- import com.sun.java.swing.UIManager;
- import com.sun.java.swing.tree.TreeCellRenderer;
- import java.awt.Color;
- import java.awt.Component;
- import java.awt.Dimension;
- import java.awt.Graphics;
-
- public class BasicTreeCellRenderer extends JLabel implements TreeCellRenderer {
- protected boolean selected;
- protected transient Icon closedIcon;
- protected transient Icon leafIcon;
- protected transient Icon openIcon;
- protected Color textSelectionColor;
- protected Color textNonSelectionColor;
- protected Color backgroundSelectionColor;
- protected Color backgroundNonSelectionColor;
- protected Color borderSelectionColor;
-
- public BasicTreeCellRenderer() {
- ((JLabel)this).setHorizontalAlignment(2);
- this.setLeafIcon(UIManager.getIcon("Tree.leafIcon"));
- this.setClosedIcon(UIManager.getIcon("Tree.closedIcon"));
- this.setOpenIcon(UIManager.getIcon("Tree.openIcon"));
- this.setTextSelectionColor(UIManager.getColor("Tree.textSelectionColor"));
- this.setTextNonSelectionColor(UIManager.getColor("Tree.textNonSelectionColor"));
- this.setBackgroundSelectionColor(UIManager.getColor("Tree.backgroundSelectionColor"));
- this.setBackgroundNonSelectionColor(UIManager.getColor("Tree.backgroundNonSelectionColor"));
- this.setBorderSelectionColor(UIManager.getColor("Tree.borderSelectionColor"));
- }
-
- public Color getBackgroundNonSelectionColor() {
- return this.backgroundNonSelectionColor;
- }
-
- public Color getBackgroundSelectionColor() {
- return this.backgroundSelectionColor;
- }
-
- public Color getBorderSelectionColor() {
- return this.borderSelectionColor;
- }
-
- public Icon getClosedIcon() {
- return this.closedIcon;
- }
-
- public Icon getDefaultClosedIcon() {
- return this.closedIcon;
- }
-
- public Icon getDefaultLeafIcon() {
- return this.leafIcon;
- }
-
- public Icon getDefaultOpenIcon() {
- return this.openIcon;
- }
-
- public Icon getLeafIcon() {
- return this.leafIcon;
- }
-
- public Icon getOpenIcon() {
- return this.openIcon;
- }
-
- public Dimension getPreferredSize() {
- Dimension retDimension = super.getPreferredSize();
- if (retDimension != null) {
- retDimension = new Dimension(retDimension.width + 3, retDimension.height);
- }
-
- return retDimension;
- }
-
- public Color getTextNonSelectionColor() {
- return this.textNonSelectionColor;
- }
-
- public Color getTextSelectionColor() {
- return this.textSelectionColor;
- }
-
- public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
- String stringValue = tree.convertValueToText(value, sel, expanded, leaf, row, hasFocus);
- ((JLabel)this).setFont(((Component)tree).getFont());
- ((JLabel)this).setText(stringValue);
- if (sel) {
- ((Component)this).setForeground(this.getTextSelectionColor());
- } else {
- ((Component)this).setForeground(this.getTextNonSelectionColor());
- }
-
- if (leaf) {
- ((JLabel)this).setIcon(this.getLeafIcon());
- } else if (expanded) {
- ((JLabel)this).setIcon(this.getOpenIcon());
- } else {
- ((JLabel)this).setIcon(this.getClosedIcon());
- }
-
- this.selected = sel;
- return this;
- }
-
- public void paint(Graphics g) {
- Color bColor;
- if (this.selected) {
- bColor = this.getBackgroundSelectionColor();
- } else {
- bColor = this.getBackgroundNonSelectionColor();
- if (bColor == null) {
- bColor = ((Component)this).getBackground();
- }
- }
-
- if (bColor != null) {
- Icon currentI = ((JLabel)this).getIcon();
- g.setColor(bColor);
- if (currentI != null && ((JLabel)this).getText() != null) {
- int offset = currentI.getIconWidth() + ((JLabel)this).getIconTextGap();
- g.fillRect(offset, 0, ((JComponent)this).getWidth() - 1 - offset, ((JComponent)this).getHeight() - 1);
- } else {
- g.fillRect(0, 0, ((JComponent)this).getWidth() - 1, ((JComponent)this).getHeight() - 1);
- }
- }
-
- if (this.selected) {
- g.setColor(this.getBorderSelectionColor());
- g.drawRect(0, 0, ((JComponent)this).getWidth() - 1, ((JComponent)this).getHeight() - 1);
- }
-
- super.paint(g);
- }
-
- public void setBackgroundNonSelectionColor(Color newColor) {
- this.backgroundNonSelectionColor = newColor;
- }
-
- public void setBackgroundSelectionColor(Color newColor) {
- this.backgroundSelectionColor = newColor;
- }
-
- public void setBorderSelectionColor(Color newColor) {
- this.borderSelectionColor = newColor;
- }
-
- public void setClosedIcon(Icon newIcon) {
- this.closedIcon = newIcon;
- }
-
- public void setLeafIcon(Icon newIcon) {
- this.leafIcon = newIcon;
- }
-
- public void setOpenIcon(Icon newIcon) {
- this.openIcon = newIcon;
- }
-
- public void setTextNonSelectionColor(Color newColor) {
- this.textNonSelectionColor = newColor;
- }
-
- public void setTextSelectionColor(Color newColor) {
- this.textSelectionColor = newColor;
- }
- }
-